let util = require('../../util/util')
const app = getApp();

Page({
    data: {
        videoList: [],
        playingStatus: false,
        nowPlayingIndex: null,
        userInfo: {}
    },
    onLoad() {
        this.getVideoInfo();
        wx.cloud.callFunction({
            name: "getUserInfo",
            success: res => {
                this.setData({
                    "userInfo.openid": res.result.openid
                })
                app.globalData.userInfo = this.data.userInfo
            }, fail: err => {
                util.errorModal(err);
            }
        })
    },
    getVideoInfo() {
        wx.cloud.callFunction({
            name: 'getVideoFun',
            data: {
                fun_name: "get_partly_video_list",
                collection_name: "videoData",
            },
            success: res => {
                let videoList = res.result.data;
                if (videoList.length > 0) {
                    this.setData({
                        videoList: videoList
                    })
                } else {
                    wx.showToast({
                        title: '没有获取到数据',
                        duration: 2000,
                        mask: true
                    })
                }
                wx.stopPullDownRefresh();
            },
            fail: err => {
                util.errorModal(err);
            }
        })
    },
    onPullDownRefresh() {
        this.getVideoInfo();
    },
    onShow() {
        this.getVideoInfo();
    },
    onReachBottom() {
        wx.showLoading({
            title: '加载中',
            mask: true,
        })
        wx.cloud.callFunction({
            name: 'getVideoFun',
            data: {
                fun_name: 'get_pulldown_refresh',
                collection_name: "videoData"
            },
            success: res => {
                wx.hideLoading();
                let new_data = res.result.data;
                if (new_data.length > 0) {
                    this.setData({
                        videoList: this.data.videoList.concat(new_data),
                    })
                } else {
                    wx.showToast({
                        title: '没有获取到数据',
                        duration: 2000,
                        mask: true
                    })
                }
            },
            fail: err => {
                util.errorModal(err);
            }
        })
    },
    addLookNum(evt) {
        wx.showLoading({
            title: '加载中',
            mask: true,
        })
        wx.cloud.callFunction({
            name: "updateVideoFun",
            data: {
                fun_name: 'update_video_look_num',
                collection_name: "videoData",
                _id: evt.currentTarget.dataset.id
            },
            success: res => {
                wx.hideLoading();
            },
            fail: err => {
                util.errorModal(err);
            }
        })
    }
})